package com.farmer.lazycoder;

import com.lanjor.commons.identity.IdNumber;
import com.lanjor.commons.properties.AppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created by LeiYun on 2016-07-09.
 */
public class Global implements ServletContextListener {

    private Logger logger = LoggerFactory.getLogger(Global.class);

    /**
     * Receives notification that the web application initialization
     * process is starting.
     * <p>
     * <p>All ServletContextListeners are notified of context
     * initialization before any filters or servlets in the web
     * application are initialized.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     *            that is being initialized
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try{
            String serverNumber= AppContext.getInstance().getProperty("server.number");
            IdNumber.init(Integer.parseInt(serverNumber));
        }catch (Exception ex){
            logger.error("初始化设置失败",ex);
        }
    }

    /**
     * Receives notification that the ServletContext is about to be
     * shut down.
     * <p>
     * <p>All servlets and filters will have been destroyed before any
     * ServletContextListeners are notified of context
     * destruction.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     *            that is being destroyed
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
